<?php
/**
 * Created by PhpStorm.
 * User: holy
 * Date: 2018/7/23
 * Time: 10:25
 */

namespace app\user\controller;


use app\user\model\UserModel;
use app\user\model\UserOrderModel;
use cmf\controller\BaseController;
use cmf\controller\UserBaseController;
use EasyWeChat\Factory;

class MemberController extends UserBaseController
{
    public function pay()
    {


        $user = cmf_get_current_user();
        $user = UserModel::get($user['id']);
        $this->assign('user', $user);
//        dump($user);
//        exit;
        $site_info = cmf_get_option('cmf_settings');
        $this->assign('site_info', $site_info);

        return $this->fetch();

    }


    public function order()
    {
        $user = cmf_get_current_user();
        $this->assign('user', $user);
        $this->assign('more', json_decode($user['more'], true));
        $orderModel = new UserOrderModel();
        $list = $orderModel->where('uid', $user['id'])->order('create_time','desc')->paginate(10);
        $this->assign('list', $list);
        $this->assign('count', $list->total());
        if ($this->request->isAjax()) {
            return $this->fetch('ajax_list');
        }
        return $this->fetch();
    }


    public function payOrder()
    {
        $config = [
            // 必要配置
            'app_id' => 'wxd8f1bbf96330e02c',
            'mch_id' => '1509241831',
            'key' => 'JCkvTDnGYbcRu0g2maYnC7XBLfKRRXIh',   // API 密钥

            // 如需使用敏感接口（如退款、发送红包等）需要配置 API 证书路径(登录商户平台下载 API 证书)
            'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径！！！！
            'key_path' => 'path/to/your/key',      // XXX: 绝对路径！！！！

            'notify_url' => '',     // 你也可以在下单时单独设置来想覆盖它
        ];
        $user = cmf_get_current_user();

        $orderModel = new UserOrderModel();

        $order_id = $this->request->param('order_id', '');

        $order = $orderModel->where('id', $order_id)->where('pay_status', 0)->where('uid', $user['id'])->find();
        if (!$order) {
            return $this->error('未找到相关订单');
        } else {
            $app = Factory::payment($config);
            $out_trade_no = $order->order_no . time();

            $result = $app->order->unify([
                'body' => '会员缴费',
                'out_trade_no' => $out_trade_no,
                'total_fee' => 1,//$order->price * 100,
                'notify_url' => 'http://www.cqeoha.com/user/public/callback', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
                'trade_type' => 'NATIVE',
            ]);
            if ($result['return_code'] == "SUCCESS") {
                $order['out_trade_no'] = $out_trade_no;
                $order->save();
                $code_url = $result['code_url'];
                $this->success('获取成功', '', ['code_url' => $code_url, 'order_no' => $order['order_no']]);
            } else {
                $this->error('获取失败', '', ['code_url' => '', 'order_no' => $order['order_no']]);

            }
        }
    }


    public function createOrder()
    {

        $config = [
            // 必要配置
            'app_id' => 'wxd8f1bbf96330e02c',
            'mch_id' => '1509241831',
            'key' => 'JCkvTDnGYbcRu0g2maYnC7XBLfKRRXIh',   // API 密钥

            // 如需使用敏感接口（如退款、发送红包等）需要配置 API 证书路径(登录商户平台下载 API 证书)
            'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径！！！！
            'key_path' => 'path/to/your/key',      // XXX: 绝对路径！！！！

            'notify_url' => '',     // 你也可以在下单时单独设置来想覆盖它
        ];
        $user = cmf_get_current_user();

        $orderModel = new UserOrderModel();
        $order = $orderModel->where('type', 0)
            ->where('uid', $user['id'])
            ->where('pay_status', 0)->order('create_time', 'desc')->find();
        if (!$order) {

            $site_info = cmf_get_option('cmf_settings');

            $order_info = [
                'order_no' => date('YmdHm') . random_int(1000, 9999),
                'price' => $site_info['user_price'],
                'create_time' => time(),
                'type' => 0,
                'pay_status' => 0,
                'uid' => $user['id'],
            ];
            $id = $orderModel->save($order_info);
            $order = $orderModel->where('id', $id)->find();
        }
        $app = Factory::payment($config);
        $out_trade_no = $order->order_no . time();

        $result = $app->order->unify([
            'body' => '会员缴费',
            'out_trade_no' => $out_trade_no,
            'total_fee' => 1,//$order->price * 100,
            'notify_url' => 'http://www.cqeoha.com/user/public/callback', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'trade_type' => 'NATIVE',
        ]);
        if ($result['return_code'] == "SUCCESS") {
            $order['out_trade_no'] = $out_trade_no;
            $order->save();
            $code_url = $result['code_url'];
            $this->success('获取成功', '', ['code_url' => $code_url, 'order_no' => $order['order_no']]);
        } else {
            $this->error('获取失败', '', ['code_url' => '', 'order_no' => $order['order_no']]);

        }
    }

    public function checkPay()
    {
        $order_no = $this->request->get('order_no');
        $orderModel = new UserOrderModel();

        $order = $orderModel->where('order_no', $order_no)->find();
        if ($order) {
            if ($order['pay_status'] == 1) {

                $this->success('获取成功');
            } else {
                $this->error('获取失败');

            }
        } else {
            $this->error('获取失败');
        }

    }
}